
export default {
    namespace: 'cart',
    state: {
        cart: {
            list: [],
            map: {}
        },
        total: {
            money: 0,
            count: 0,
            chooseAll: true
        }
    },
    reducers: {
        reset() {
            return {
                cart: {
                    list: [],
                    map: {}
                },
                total: {
                    money: 0,
                    chooseAll: true
                }
            }
        },
        updateCart(state, { payload }) {
            const { list, map } = payload
            return {
                ...state,
                cart: {
                    ...state.cart,
                    list, map
                }
            }
        },
        sum(state) {
            let money = 0
            let count = 0
            let chooseAll = true
            let list = state.cart.list
            list.forEach(item => {
                if (item.checked === false) {
                    chooseAll = false
                }
                count += item.count
                money += item.count * item.price
            })
            return {
                ...state,
                total: { 
                    money: money.toFixed(2), 
                    count,
                    chooseAll 
                }
            }
        }
    },
    effects: {
        /*
            1. 加入购物车
                1. 初次加入
                2. 增加数量
            2. 减少购物车数量
                如果数量小于1不能继续减少
            3. 勾选商品
            4. 删除商品
            5. 清空购物车
                1. 创建订单后
        */

        /*
            1. 加入购物车
                1. 初次加入
                2. 增加数量
        */
        *pushCart({ payload }, { put, select }) {
            const state = yield select()
            let commcodiy = state.cart.cart.map[payload._id]
            let list = [ ...state.cart.cart.list ]
            let map = { ...state.cart.cart.map }
            if (commcodiy !== undefined) {
                // 修改属性
                console.log('增加数量')
                map[payload._id] = {
                    ...map[payload._id],
                    ...payload,
                }

                list = list.map(item => {
                    if (item._id === payload._id) {
                        return {...map[payload._id]}
                    }
                    return {...item}
                })
            } else {
                // 新加入购物车
                console.log('新加入购物车')
                payload.count = 1
                // 是否勾选
                payload.checked = true

                list.push(payload)
                map[payload._id] = payload
            }

            yield put({ type: 'updateCart', payload: { list, map } })
            yield put({ type: 'sum' })
        },
        // 取消全选
        *cleanChecked({ }, { put, select }) {
            const state = yield select()
            let list = [ ...state.cart.cart.list ]
            let map = { ...state.cart.cart.map }
            list = list.map(item => {
                item.checked = !state.cart.total.chooseAll
                map[item._id] = {...item}
                return {...item}
            })
            yield put({ type: 'updateCart', payload: { list, map } })
            yield put({ type: 'sum' })
        },
        *removeCart({ payload }, { put, select }) {
            const state = yield select()
            let list = [ ...state.cart.cart.list ]
            let map = {  }
            let newList = []

            list.forEach(item => {
                if (payload._id !== item._id) {
                    map[item._id] = {...item}
                    newList.push({...item})
                }
            })

            yield put({ type: 'updateCart', payload: { list: newList, map } })
            yield put({ type: 'sum' })
        },
    }
}
